<?php

/*
Name: requirement_details.php
Author: Jacob Siegel [jacob707@gmail.com]
Description: Requirement Details Page
Last Modified: 10/19/2008 by Justin Williamson
*/

require_once "includes/config.php";
require_once INCLUDE_DIR."/header.inc.php";
require_once INCLUDE_DIR."/footer.inc.php";
require_once INCLUDE_DIR."/db.inc.php";
require_once INCLUDE_DIR."/status.inc.php";

if( $_GET[ 'add_new' ] ) {
	header( "Location: requirement_details.php?action=edit&id=".nextAuto( "requirement" ) );
}

$req = new HTML_Template_IT( TEMPLATE_DIR );
if( $_GET[ 'action' ] == "edit" )
	$req->loadTemplatefile( "requirement_details_edit.tpl.html", true, true );
else
	$req->loadTemplatefile( "requirement_details_view.tpl.html", true, true );
$id = $_GET['id'];

$query = "SELECT `id`, `short`, `status`, `description`, `comments` FROM `requirement` WHERE `id` = ".$id;

$query = mysql_query( $query );
$result = mysql_fetch_object( $query );
$req->setVariable( "REQUIREMENT_ID", $_GET[ 'id' ] );
if( $_GET[ 'action'] == "edit" ) {
	$req->setVariable( "REQUIREMENT_SHORT_DESCRIPTION", $result->short );
	$req->setVariable( "REQUIREMENT_DETAILS", $result->description );
	$req->setVariable( "REQUIREMENT_STATUS", getStatusString( $result->status ) );
	$req->setVariable( "REQUIREMENT_COMMENTS", br2nl( $result->comments ) );
}
else {
	$req->setVariable( "REQUIREMENT_SHORT_DESCRIPTION", nl2br( $result->short ) );
	$req->setVariable( "REQUIREMENT_DETAILS", nl2br( $result->description ) );
	$req->setVariable( "REQUIREMENT_STATUS", getStatusString( $result->status ) );
	$req->setVariable( "REQUIREMENT_COMMENTS", $result->comments );
}

if( $_GET[ 'action' ] == "edit" ) {
	foreach( $valid_statuses as $vs ) {
		$req->setVariable( "REQUIREMENT_STATUS_ID", $vs );
		$req->setVariable( "REQUIREMENT_STATUS_NAME", getStatusString( $vs ) );
		if( $result->status == $vs ) $req->setVariable( "REQUIREMENT_STATUS_SELECTED", "selected=\"true\"" );
		else $req->setVariable( "REQUIREMENT_STATUS_SELECTED", "" );
		$req->parse( "status" );
	}
	$selected = array();
	$query = "SELECT `test_case_id` FROM `references` WHERE `requirement_id` = ".$id;
	$query = mysql_query( $query );
	while( $result = mysql_fetch_object( $query ) ) {
		array_push( $selected, $result->test_case_id );
	}
	$query = "SELECT `short`, `id` FROM `test_case` WHERE 1";
	$query = mysql_query( $query );
	while( $result = mysql_fetch_object( $query ) ) {
		$req->setVariable( "TEST_CASE_ID", $result->id );
		$req->setVariable( "TEST_CASE_ID_NAME", $result->id.": ".$result->short );
		if( in_array( $result->id, $selected ) ) $req->setVariable( "TEST_CASE_IDS_SELECTED", "selected=\"true\"" );
		else $req->setVariable( "TEST_CASE_IDS_SELECTED", "" );
		$req->parse( "test_cases" );
	}
}
else {
	$query = "SELECT `test_case_id` FROM `references` WHERE `requirement_id` = ".$id;
	$query = mysql_query( $query );
	while( $result = mysql_fetch_object( $query ) ) {
		$req->setVariable( "TEST_CASE_ID", $result->test_case_id );
		$req->parse( "test_cases" );
	}
}

$header->show();
$req->show();
$footer->show();
?>
